package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Dictionary;
import org.ahuanlien.crm.service.IDictionaryService;
import org.ahuanlien.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {

	@Autowired
	private IDictionaryService dictionaryService;

	//打开页面和数据获取分开
	@RequestMapping("view")
	public String view() {
		return "dictionary/view";
	}

	@RequestMapping("selectRootDictionary")
	@ResponseBody
	public List<Dictionary> selectRootDictionary( ) {
		List<Dictionary> dictionaries = dictionaryService.selectRootDictionary();
		return dictionaries;
	}

	@RequestMapping("selectDictionaryItemBySn")
	@ResponseBody
	public List<Dictionary> selectDictionaryItemBySn(String sn) {
		List<Dictionary> dictionaries = dictionaryService.selectDictionaryItemBySn(sn);
		return dictionaries;
	}

	//回显字典
	@RequestMapping("selectDictionaryItemById")
	@ResponseBody
	public Dictionary selectDictionaryItemById(Long id) {
		Dictionary dictionary = dictionaryService.selectDictionaryItemById(id);
		return dictionary;
	}

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Dictionary dictionary) {
		try {
			dictionaryService.saveOrUpdate(dictionary);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
	@RequestMapping("saveOrUpdateSn")
	@ResponseBody
	public JSONResult saveOrUpdateSn(Dictionary dictionary) {
		try {
			dictionaryService.saveOrUpdateSn(dictionary);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	@RequestMapping("delete")
	@ResponseBody
	public JSONResult delete(Long id) {
		try {
			dictionaryService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
	@RequestMapping("deleteSn")
	@ResponseBody
	public JSONResult deleteSn(Long id) {
		try {
			dictionaryService.deleteByPrimaryKeySn(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
}
